# 夜魅小说网平台数据爬去

import time
import re
import insert_sql
from selenium import webdriver
username = ['']
while 1:
    for s in range(0,2):
        driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
        url = "https://inovel.818tu.com/backend/referral_links/index"
        driver.get('{}'.format(url))
        driver.maximize_window()
        driver.implicitly_wait(10)
        driver.find_element_by_id('username').clear()
        driver.find_element_by_id('username').send_keys('{0}'.format(username[s]))
        #定位密码
       
        driver.find_element_by_name('password').clear()
        driver.find_element_by_name('password').send_keys('{0}'.format(''))
        #点击登录
        driver.find_element_by_id('loginbtn').click()
        # 获取页面源码
        # body = driver.page_source
        # print(body)
        time.sleep(3)
        for m in range(2):
            try:
                driver.find_element_by_xpath("//button[text()='我知道了']").click()
                time.sleep(2)
            except Exception:
                print("处理完弹出框问题")
        #点击推广管理
        driver.find_element_by_xpath('//*[@id="menu-ad"]').click()
        #点击推广链接显示推广页面
        driver.find_element_by_xpath('//*[@id="menu-ad-submenu"]/li[1]/a').click()
        #获取总页面
        number = driver.find_element_by_xpath('//span[1]/em[2]').text
        j = int(number) +1
        for x in range(1,j):
            # time.sleep(2)
            driver.find_element_by_xpath('//span[2]/input').clear()
            driver.find_element_by_xpath('//span[2]/input').send_keys('{0}'.format(x))
            driver.find_element_by_xpath('//span[2]/button').click()
            body = driver.page_source
            # print(body)
            html = body.replace('\n','').replace('\r','').replace('\t','')
            # 公众平台
            Public_platform_list = re.findall('<span class="link-desc">(.*?)</span>',html)
            #推广链接url
            url_list = re.findall('<span class="text-primary" id=".*?">(.*?)</span>',html)
            #创建时间
            Creation_time_list = re.findall('创建时间:(.*?)</div>',html)
            # 推荐的小说名
            Novel_name_list = re.findall('<a href="/backend/novels/view/.*?">(.*?)</a>',html)
            #小说的章节
            # Novel_name_chapter_list = re.findall('<div style="line-height:1.7em">.*?</a>(.*?)</div>',html)
            #原文点击次数
            Number_clicks_list = re.findall('<span class="counter-field" data-field="clicks">(.*?)</span>',html)
            #新增阅读用户
            New_reading_users_list = re.findall('<span class="counter-field" data-field="member_count">(.*?)</span>',html)

            #新增关注
            New_attention_list = re.findall('<span class="counter-field" data-field="subscribe_count">(.*?)</span>',html)

            #充值金额
            Recharge_amount_list = re.findall('<span class="counter-field" data-field="subscribe_rate">.*?</td>.*?<td class="text-right">(.*?)</td>',html)
            #充值笔数
            Recharge_number_list = re.findall('<p><span class="counter-field">(.*?)</span> 笔</p>.*?<p><span class="counter-field">(.*?)</span> %</p>',html)
            # #充值比例
            # Recharge_proportion = re.findall('<p><span class="counter-field">(.*?)%</p>',html)
            for i in range(len(Public_platform_list)):

                Public_platform = Public_platform_list[i]
                url = url_list[i]
                Creation_time = Creation_time_list[i]
                Novel_name = Novel_name_list[i]
                # Novel_name_chapter = Novel_name_chapter_list[i]
                Number_clicks = Number_clicks_list[i]
                New_reading_users = New_reading_users_list[i]
                New_attention = New_attention_list[i]
                Recharge_amount = Recharge_amount_list[i]
                Recharge_number = Recharge_number_list[i][0]
                # 判断数据库中是否已存在此推广链接

                if s == 0:
                    a = insert_sql.select_novel_mysql(url=url_list[i])
                    if a == 1:
                        # 存在 替换原有数据
                        insert_sql.Update_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,New_reading_users,New_attention,Recharge_amount,Recharge_number)
                        print("夜魅小说网 更新数据")
                    else:
                        # 否则  插入数据
                        insert_sql.insert_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,New_reading_users,New_attention,Recharge_amount,Recharge_number)
                        print("夜魅小说网 插入数据")
                else:
                    a = insert_sql.liulang_select_novel_mysql(url=url_list[i])
                    if a == 1:
                        # 存在 替换原有数据
                        insert_sql.liulang_Update_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,New_reading_users,New_attention,Recharge_amount,Recharge_number)
                        print("流浪小说 更新数据")
                    else:
                        # 否则  插入数据
                        insert_sql.liulang_insert_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,New_reading_users,New_attention,Recharge_amount,Recharge_number)
                        print("流浪小说 插入数据")
        driver.quit()
        #休眠
    print("休眠10分钟")
    time.sleep(600)